//
//  CustomTableViewCell.m
//  10-CustomTableViewCell
//
//  Created by 曹曹 on 16/7/27.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "CustomTableViewCell.h"
#import "UIView+Frame.h"

#define kContentWidth self.contentView.frame.size.width
#define kContentHeight self.contentView.frame.size.height

@interface CustomTableViewCell ()
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *followLabel;
@property (nonatomic, strong) UIView *customView;
@end

@implementation CustomTableViewCell
// 自定义TableViewcell的初始化方法
// initWithStyle:reuseIdentifier:
// 注意: 不要使用initWithFrame:
#if 0
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
#endif
#warning 初始化方法中, 只负责创建
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // TableViewCell默认大小 宽320 高44
        // 1.在初始化时, cell的frame始终是初始值, 不能给自定义视图布局
        // 添加子view
        NSLog(@"初始化时cell的frame是:%@", NSStringFromCGRect(self.frame));
        // _customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 22)];
        _leftImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_leftImageView];
        _titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        _detailLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_detailLabel];
        _detailLabel.numberOfLines = 0;
        _followLabel = [[UILabel alloc] init];
        [_detailLabel addSubview:_followLabel];
        // 3.创建的子视图应该添加在哪个父视图
        // contentView显示内容
        // [self.contentView addSubview:_customView];
    }
    return self;
}
#warning layoutSubviews中, 负责给视图布局
// 有内容或者坐标改变时, 就会触发该方法
// 初始化不能在这里写(会创建多个重复的叠到一起)
// 2.布局子视图及本身
- (void)layoutSubviews {
    // 一定要写super
    [super layoutSubviews];
    NSLog(@"%s时cell的frame是:%@", __FUNCTION__, NSStringFromCGRect(self.frame));
    // 不要使用cell的frame去进行视图的布局
    // 使用contentView的frame进行布局
    // _customView.frame = CGRectMake(5, 5, kContentWidth / 3, kContentHeight - 10);
    // 不要在label上添加视图, 不会显示出来
    CGFloat leftMargin = 5;
    CGFloat rigtMargin = 5;
    CGFloat topMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat middleMargin = 5;
    CGFloat titleLabelHeight = 44;
    _leftImageView.frame = CGRectMake(leftMargin, topMargin, kContentWidth / 3, kContentHeight - topMargin - bottomMargin);
    CGFloat titleLabelX = _leftImageView.gx_x + _leftImageView.gx_width + middleMargin;
    CGFloat titleLabelW = self.contentView.gx_width - titleLabelX - rigtMargin;
    _titleLabel.frame = CGRectMake(titleLabelX, topMargin, titleLabelW, titleLabelHeight);
    CGFloat detailY = _titleLabel.gx_y + _titleLabel.gx_height + middleMargin;
    CGFloat detailH = self.contentView.gx_height - titleLabelHeight - topMargin - middleMargin - bottomMargin;
    _detailLabel.frame = CGRectMake(titleLabelX, detailY, titleLabelW, detailH);
    CGFloat followX = _detailLabel.gx_width * 3 / 5;
    CGFloat followW = _detailLabel.gx_width * 2 / 5;
    CGFloat followH = _detailLabel.gx_height / 3;
    CGFloat followY = _detailLabel.gx_height * 2 / 3;
#pragma mark - 坐标转换
    // 1.判断父视图是不是_detailLabel
    if (_followLabel.superview == _detailLabel) {
        _followLabel.frame = CGRectMake(followX, followY, followW, followH);
        // 视图坐标系转换
        CGRect newRect = [_followLabel.superview convertRect:_followLabel.frame toView:self.contentView];
        _followLabel.frame = newRect;
        [self.contentView addSubview:_followLabel];
    }
    // 2.改变_followLabel的父视图
//    [_detailLabel addSubview:_followLabel];
//    _followLabel.frame = CGRectMake(followX, followY, followW, followH);
//    // 视图坐标系转换
//    CGRect newRect = [_followLabel.superview convertRect:_followLabel.frame toView:self.contentView];
//    _followLabel.frame = newRect;
//    [self.contentView addSubview:_followLabel];
}

- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _leftImageView.image = [UIImage imageNamed:newsObject.newsImage];
    _titleLabel.text = newsObject.newsTitle;
    _detailLabel.text = newsObject.newsDetail;
    _followLabel.text = newsObject.newsFollow;
}

- (void)setModel:(NewsModel *)model {
    _model = model;
    _leftImageView.image = [UIImage imageNamed:@"猴赛雷"];
    _titleLabel.text = model.title;
    _detailLabel.text = model.summary;
    _followLabel.text = model.lastUpdateTime;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
